package org.tbwork.utility.httprr;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tbwork.utility.httprr.enums.RequestType;
 
/**In HttpRR, this Request contains common settings of HTTP Request.<br>
 * Are you looking for RequestConfig? In this tool, it is defined in HttpConfig and initialized with HttpRR executor.
 * @author Tommy.Tang
 *
 */
public final class Request 
{   
	private String url =""; 
	private RequestType requestType = RequestType.GET; 
	private HttpEntity postEntity = null; 
	private List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
	private Map<String, String> headers = new HashMap<String, String>(); 	
	private Map<String, String> subtmits = new HashMap<String, String>();
	private final static Logger logger=LoggerFactory.getLogger(Request.class);
	private Map<String, Cookie> requestCookiesMap = new HashMap<String, Cookie>();
	
	public Request()
	{ 
	} 
	
	public Request(String url)
	{
		setUrl(url);
	} 
	
	public Request(String url, RequestType requestType)
	{
		setUrl(url);
		setRequestType(requestType);
	} 
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		if(!url.contains("http"))
			url = "http://" + url;
		this.url = url;
	}

    public void addHeader(String key,String value)
    {
    	headers.put(key, value);
    }
    public void setHeader(String key,String value)
    {
    	headers.put(key, value);
    }
    public String getHeader(String key)
    {
    	return headers.get(key);
    }
    
    /** Works same as {@link #setSubmitData(String, String)}
     * @param key
     * @param value
     */
    public void addSubmitData(String key,String value)
    {
    	subtmits.put(key, value);
    }
    
    /** Works same as {@link #addSubmitData(String, String)}
     * @param key
     * @param value
     */
    public void setSubmitData(String key,String value)
    {
    	subtmits.put(key, value);
    }
    
    public void setPostEntity(HttpEntity entity)
    {
    	postEntity = entity;
    } 
	
    public void setJSONEntity(String jsonString)
    {
    	postEntity = createJSONEntity(jsonString);
    }
    
	public RequestType getRequestType() {
		return requestType;
	}
	
	public void setRequestType(RequestType requestType) {
		this.requestType = requestType;
	}
  
	public void addCookie(Cookie cookie) {
		String key = cookie.getDomain() + "/" + cookie.getName();
		requestCookiesMap.put(key, cookie);
	}

	public void addCookieList(List<HttpCookie> cookieList) {
		for (BasicClientCookie item : cookieList) {
			addCookie(item);
		}
	}
	
	protected Map<String,Cookie> getCookieMap()
	{
		return this.requestCookiesMap;
	}
	 

	public List<NameValuePair> getNvps() {
		return nvps;
	}

	public void setNvps(List<NameValuePair> nvps) {
		this.nvps = nvps;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public Map<String, String> getSubtmits() {
		return subtmits;
	}

	public void setSubtmits(Map<String, String> subtmits) {
		this.subtmits = subtmits;
	}

	public Map<String, Cookie> getRequestCookiesMap() {
		return requestCookiesMap;
	}

	public void setRequestCookiesMap(Map<String, Cookie> requestCookiesMap) {
		this.requestCookiesMap = requestCookiesMap;
	}

	public HttpEntity getPostEntity() {
		return postEntity;
	}

	public static Logger getLogger() {
		return logger;
	}	
	
	
	private HttpEntity createJSONEntity(String content)
	{
		ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
		return new StringEntity(content, contentType);
	}
}  